package com.kt.mes.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.kt.mes.domain.StockBin;
import com.kt.mes.domain.StorageInRecord;
import com.kt.mes.domain.StorageOutRecord;
import com.kt.mes.mapper.StockBinMapper;
import com.kt.mes.mapper.StorageInRecordMapper;
import com.kt.mes.service.IStorageOutRecordService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.kt.mes.mapper.StockBinListMapper;
import com.kt.mes.domain.StockBinList;
import com.kt.mes.service.IStockBinListService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 料仓详情Service业务层处理
 * 
 * @author lzc
 * @date 2021-01-26
 */
@Service
public class StockBinListServiceImpl implements IStockBinListService 
{


    @Autowired
    private StockBinListMapper stockBinListMapper;
    @Autowired
    private StorageInRecordMapper storageInRecordMapper;

    @Autowired
    private StockBinMapper stockBinMapper;
    @Autowired
    private IStorageOutRecordService storageOutRecordService;
    /**
     * 查询料仓详情
     * 
     * @param id 料仓详情ID
     * @return 料仓详情
     */
    @Override
    public StockBinList selectStockBinListById(Long id)
    {
        return stockBinListMapper.selectStockBinListById(id);
    }

    /**
     * 查询料仓详情列表
     * 
     * @param stockBinList 料仓详情
     * @return 料仓详情
     */
    @Override
    public List<StockBinList> selectStockBinListList(StockBinList stockBinList)
    {
        return stockBinListMapper.selectStockBinListList(stockBinList);
    }

    /**
     * 新增料仓详情
     * 
     * @param stockBinList 料仓详情
     * @return 结果
     */
    @Override
    @Transactional
    public int insertStockBinList(StockBinList stockBinList)
    {
        stockBinList.setCreateTime(DateUtils.getNowDate());
        //入库记录生成
        StorageInRecord storageInRecord=new StorageInRecord();
        storageInRecord.setRkId("RK"+DateUtils.dateTimeNow());
        //storageInRecord.setPlId(); //产线
        storageInRecord.setmId(stockBinList.getmId());
        storageInRecord.setRkQty(stockBinList.getQty().intValue());
        storageInRecord.setCreateTime(DateUtils.getNowDate());
        storageInRecord.setRkType("料仓补料");
        //位置： 产线：1  料仓：L-1-1

        //查询产线信息
        StockBin sB= stockBinMapper.selectStockBinById(stockBinList.getTsbId());


        storageInRecord.setRkPos("产线："+sB.getPlId()+"——"+"料仓："+sB.getSbId());

        storageInRecordMapper.insertStorageInRecord(storageInRecord);


        return stockBinListMapper.insertStockBinList(stockBinList);
    }

    /**
     * 修改料仓详情
     * 
     * @param stockBinList 料仓详情
     * @return 结果
     */
    @Override
    public int updateStockBinList(StockBinList stockBinList)
    {
        stockBinList.setUpdateTime(DateUtils.getNowDate());
        return stockBinListMapper.updateStockBinList(stockBinList);
    }

    /**
     * 批量删除料仓详情
     * 
     * @param ids 需要删除的料仓详情ID
     * @return 结果
     */
    @Override
    public int deleteStockBinListByIds(Long[] ids)
    {
        return stockBinListMapper.deleteStockBinListByIds(ids);
    }

    /**
     * 删除料仓详情信息
     * 
     * @param id 料仓详情ID
     * @return 结果
     */
    @Override
    public int deleteStockBinListById(Long id)
    {
        return stockBinListMapper.deleteStockBinListById(id);
    }

    @Override
    public Map<String, Object> selectStockBinSum(StockBinList stockBinList) {
        return stockBinListMapper.selectStockBinSum(stockBinList);
    }
    @Override
    public void outStockBinByTsbId(Long tsbId){



        //查询存储情况
        StockBinList sbl_query=new StockBinList();
        sbl_query.setTsbId(tsbId);
        List<StockBinList> list_sbls= selectStockBinListList(sbl_query);



        if(!list_sbls.isEmpty()){
            //去一个减去库存
            StockBinList sbl_frist= list_sbls.get(0);

            Long qty =sbl_frist.getQty();

            if(qty-1==0){
                deleteStockBinListById(sbl_frist.getId());
            }
            else if(qty-1>0){
                sbl_frist.setQty(qty-1);
                updateStockBinList(sbl_frist);
            }

           //出库记录
            StorageOutRecord storageOutRecord=new StorageOutRecord();
            storageOutRecord.setCkId("CK"+DateUtils.dateTime()+tsbId+"");
            storageOutRecord.setmId(sbl_frist.getmId());
            storageOutRecord.setCkType("料仓出料");
            storageOutRecord.setCkQty(1L);
            storageOutRecord.setCkPos("料仓"+stockBinMapper.selectStockBinById(tsbId).getSbId());
            storageOutRecord.setCreateTime(new Date());

            storageOutRecordService.insertStorageOutRecord(storageOutRecord);

        }


    }



}
